package com.easylinkin.linkappapi.openapi.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easylinkin.linkappapi.common.model.RequestModel;
import com.easylinkin.linkappapi.lobar.dto.UserProjectDTO;
import com.easylinkin.linkappapi.lobar.dto.app.LobarUserDTO;
import com.easylinkin.linkappapi.lobar.dto.screen.LobarScreenDTO;
import com.easylinkin.linkappapi.lobar.service.LobarScreenService;
import com.easylinkin.linkappapi.lobar.service.UserProjectService;
import com.easylinkin.linkappapi.lobar.util.PercentageUtils;
import com.easylinkin.linkappapi.tenant.dto.LinkappTenantDTO;
import com.easylinkin.linkappapi.tenant.entity.LinkappTenant;
import com.easylinkin.linkappapi.tenant.sevice.LinkappTenantService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

/**
 * @Author: kan yuanfeng
 * @Date: 2022/11/21 13:37
 * @Description: 花名册
 */
@RestController
@RequestMapping("openapi/user")
@Api(tags = "花名册openApi")
public class OpenUserProjectController {
  @Autowired
  private UserProjectService userProjectService;
  @Autowired
  private LobarScreenService lobarScreenService;
  @Autowired
  private LinkappTenantService linkappTenantService;

  /**
   * 当前现场人员统计/app企业级
   */
  @PostMapping("projectUser")
  public RestMessage countProjectUser(@RequestBody UserProjectDTO userProjectDTO){
    //开始时间
    userProjectDTO.setStartTime(DateUtil.beginOfDay(userProjectDTO.getStartTime()));
    //结束时间
    userProjectDTO.setEndTime(DateUtil.endOfDay(userProjectDTO.getEndTime()));
    RequestModel<UserProjectDTO> requestModel = new RequestModel<>();
    requestModel.setCustomQueryParams(userProjectDTO);
    requestModel.setPage(new Page(0,-1));
    IPage<LinkappTenantDTO> iPage = lobarScreenService.countProject(requestModel);
    List<LinkappTenantDTO> pageRecords = iPage.getRecords();
    return RestBuilders.successBuilder().data(pageRecords.stream().sorted(Comparator.comparing(LinkappTenantDTO::getAttendance).thenComparing(LinkappTenantDTO::getIn).reversed()).collect(Collectors.toList())).build();
  }

  /**
   * 当前现场人员统计总人数
   */
  @PostMapping("countProjectUser")
  public RestMessage countAll(@RequestBody UserProjectDTO userProjectDTO){
    //开始时间
    userProjectDTO.setStartTime(DateUtil.beginOfDay(userProjectDTO.getStartTime()));
    //结束时间
    userProjectDTO.setEndTime(DateUtil.endOfDay(userProjectDTO.getEndTime()));
    LobarUserDTO lobarUserDTO = lobarScreenService.countAll(userProjectDTO);
    return RestBuilders.successBuilder().data(lobarUserDTO).build();
  }

  /**
   * 折线图(项目级)
   */
  @PostMapping("countByProject")
  public RestMessage countByProject(@RequestBody UserProjectDTO userProjectDTO){
    Assert.notNull(userProjectDTO.getProjectId(),"项目id不能为null");
    getTenantId(userProjectDTO);
    List<LobarUserDTO> lobarUserDTO = lobarScreenService.countByProject(userProjectDTO);
    return RestBuilders.successBuilder().data(lobarUserDTO).build();
  }

  /**
   * 年龄籍贯分布
   */
  @PostMapping("countUser")
  public RestMessage countUser(@RequestBody UserProjectDTO userProjectDTO){
    Assert.notNull(userProjectDTO.getProjectId(),"项目id不能为null");
    getTenantId(userProjectDTO);
    Map<String, List<LobarScreenDTO>> map = lobarScreenService.countAllUser(userProjectDTO.getTenantId(),null);
    return RestBuilders.successBuilder().data(map).build();
  }

  /**
   * 工种统计（企业级）
   */
  @PostMapping("workType")
  public RestMessage workType(@RequestBody UserProjectDTO userProjectDTO){
    Assert.notNull(userProjectDTO.getProjectId(),"项目id不能为null");
    getTenantId(userProjectDTO);
    Map<String, List<LobarScreenDTO>> map = lobarScreenService.countEnWorkType(userProjectDTO.getTenantId());
    return RestBuilders.successBuilder().data(map).build();
  }

  /**
   * @Description: 根据条件，分页(不分页)查询非黑名单人员
   * @author kan yuanfeng
   * @date 2020/11/04 11:42
   */
  @PostMapping("list")
  @ApiOperation("根据条件，分页(不分页)查询")
  public RestMessage queryListByPage(@RequestBody RequestModel<UserProjectDTO> requestModel){
    Assert.notNull(requestModel.getCustomQueryParams(), "customQueryParams 不能为空");
    Assert.notNull(requestModel.getPage(), "page 不能为空");
    IPage<UserProjectDTO> record =  userProjectService.queryEnListByPage(requestModel);
    return RestBuilders.successBuilder().data(record).build();
  }

  private void getUsers(List<LobarUserDTO> list, List<LobarUserDTO> dtoList,String name) {
    List<LobarUserDTO> userDTOS = dtoList.stream().filter(d -> name.equals(d.getName()))
        .collect(Collectors.toList());
    LobarUserDTO lobarUserDTO = new LobarUserDTO();
    lobarUserDTO.setName(userDTOS.get(0).getName());
    int in = userDTOS.stream().mapToInt(u -> u.getIn()).sum();
    int on = userDTOS.stream().mapToInt(u -> u.getOn()).sum();
    int attendance = userDTOS.stream().mapToInt(u -> u.getAttendance()).sum();
    lobarUserDTO.setIn(in);
    lobarUserDTO.setOn(on);
    lobarUserDTO.setAttendance(attendance);
    lobarUserDTO.setPart(PercentageUtils.computePercentTwo(attendance,in));
    list.add(lobarUserDTO);
  }

  /**
   * 获取tenantId
   * @param userProjectDTO
   */
  private void getTenantId(UserProjectDTO userProjectDTO) {
    LinkappTenant tenant = linkappTenantService
        .getOneByProjectId(userProjectDTO.getProjectId());
    if (null != tenant){
      userProjectDTO.setTenantId(tenant.getId());
    }
  }
}
